use regex::Regex;

pub fn filter_ip_from(whatever: String) -> Option<String> {
    let re = Regex::new(r#"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"#).unwrap();
    let result = if let Some(mat) = re.find(&whatever) {
        let ip = &(whatever.clone())[mat.start()..mat.end()];
        Some(ip.to_owned())
    } else {
        None
    };
    result
}

#[cfg(test)]
mod tests {
    #[test]
    fn test_filter_ip_from() {
        use crate::utils::ip_filter;
        use crate::utils::logutil;

        logutil::global_log_init("DEBUG");
        let my_string = r#"xxasdfadf 112.113.224.134"#;
        let id = ip_filter::filter_ip_from(my_string.to_owned());
        assert_eq!(Some(r#"112.113.224.134"#.to_owned()), id);
    }
}
